package com.example.test;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<ImageView> imageViews = new ArrayList<>();
    ViewPager viewPager;
    TextView startSofter;
    TextView setPort;
    ImageView indicator;
    ImageView[] indicators;
    LinearLayout indicatorGroup;
    SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.ViewPager);
        startSofter = findViewById(R.id.startSofter);
        setPort = findViewById(R.id.setPort);
        indicatorGroup = findViewById(R.id.indicatorGroup);
        initView();
    }

    public void initView(){
        ImageView img1 = new ImageView(this);
        img1.setBackgroundResource(R.drawable.yingdao1);
        imageViews.add(img1);
        ImageView img2 = new ImageView(this);
        img2.setBackgroundResource(R.drawable.yingdao2);
        imageViews.add(img2);
        ImageView img3 = new ImageView(this);
        img3.setBackgroundResource(R.drawable.yingdao3);
        imageViews.add(img3);
        ImageView img4 = new ImageView(this);
        img4.setBackgroundResource(R.drawable.yingdao4);
        imageViews.add(img4);
        ImageView img5= new ImageView(this);
        img5.setBackgroundResource(R.drawable.yingdao5);
        imageViews.add(img5);

        indicators = new ImageView[imageViews.size()];
        for (int i=0;i<indicators.length;i++){
            indicator = new ImageView(this);
            indicator.setLayoutParams(new LinearLayout.LayoutParams(45,45));
            indicator.setPadding(5,5,5,5);
            indicator.setId(i);
            if (i==0){
                indicator.setBackgroundResource(R.drawable.white);
            }else{
                indicator.setBackgroundResource(R.drawable.gray);
            }
            indicators[i] = indicator;
            indicatorGroup.addView(indicators[i]);
        }

        for (int i=0;i<indicators.length;i++){
            int finalI = i;
            indicators[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPager.setCurrentItem(finalI);
                }
            });
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i =0;i<indicators.length;i++){
                    if (i==position){
                        indicators[i].setBackgroundResource(R.drawable.white);
                    }else{
                        indicators[i].setBackgroundResource(R.drawable.gray);
                    }
                }
                if (position==indicators.length-1){
                    show();
                }else{
                    close();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setAdapter(new myAdapter(imageViews));

    }

    public void show(){
        setPort.setVisibility(View.VISIBLE);
        startSofter.setVisibility(View.VISIBLE);
    }
    public void close(){
        setPort.setVisibility(View.GONE);
        startSofter.setVisibility(View.GONE);
    }

    public void setStartSofter(View view){
        Intent intent = new Intent(MainActivity.this,login.class);
        sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("isFirst",true);
        editor.apply();
        startActivity(intent);
    }

}


class myAdapter extends PagerAdapter{
    ArrayList<ImageView> list;
    public myAdapter(ArrayList<ImageView> list){
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        container.addView(list.get(position));
        return list.get(position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(list.get(position));
    }
}